var uid = getParameter("uid");
var now = new Date();
var code=getParameter("code");
$(function() {
	if(uid){
	charts(now);
	}else{
		getToken(code);
	}
});
function getToken(code_){
	var urlStr = serverPath + "/server/doctor/diabetes/login.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"",'+
	    '	   "method":"taipinglogin",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "code":"'+code_+'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	UseAjax(urlStr, jsonStr, document.cookie, getUid, '1');

};

function getUid(data) {
	if(data.resultcode=='0'){
		uid = data.uid;
		charts(now);
	}else{
		alert(data.resultmsg);	
	}
}
function charts(now) {
	var urlStr = serverPath + "/app/doctor/diabetes/mealView.do"; 
    //var jsonStr ='{method:"getMealTable",UId:"' + UId + '",date:"' + now.format("yyyy-MM-dd") + '",day:"1"}';
    var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"'+uid+'",'+
	    '	   "method":"getMealTable",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "uid":"'+uid+'",'+
	    '            "date":"' + now.format("yyyy-MM-dd") +'",'+
	    '            "day":"1"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
    UseAjax(urlStr, jsonStr, document.cookie, rt, '1');
}
function rt(data) {
	var datas = new Array();
	var wi = data.resultlist[0].records;
	if(wi.length > 0){
		datas=[isnull(wi[0].breakfast),isnull(wi[0].lunch),isnull(wi[0].dinner),isnull(wi[0].snack)];
	}
    var target =  data.target;
	var dates = [ "早餐","午餐","晚餐","加餐" ];
	$('#container').highcharts(
			{

				title : {
					text : ''
				},
				subtitle : {
					text :  now.format("MM-dd")
				},
				xAxis : {
					
					  labels: { 
						  },
					 
					gridLineWidth : 0.5,
					gridLineDashStyle : 'Dash',// 样式 虚线
					tickPixelInterval : 50,
					categories : dates
//	                labels:{
//	                    x:45,//调节x偏移
//	                    //y:-35,//调节y偏移
//	                    //rotation:25//调节倾斜角度偏移
//	                 }
				},
				yAxis : {
					min : 0, // 固定列最小值
					max : 3000,
					title : {
						//align: 'high',
		                //offset: 10,
		                text: '千卡'
		                //rotation: 0,
		                //y: 20	
					},labels : {
						formatter : function() {
							return "" + this.value ;
						}
					} ,plotLines: [{ 
						value: target,//標示線 
						width: 1, 
						color:'#808080',
						label:{
						     text:target,     //标签的内容
						     align:'left',                //标签的水平位置，水平居左,默认是水平居中center
						     x:-40                         //标签相对于被定位的位置水平偏移的像素，重新定位，水平居左10px
						    }
						  }]
					
				},
				tooltip : {
					formatter : function() {
					}
				},
				plotOptions : {
					line : {
						dataLabels : {
							enabled : true
						},
						marker: {
		                    enabled: true
		                },
						enableMouseTracking : false
					}
				},
				credits : {
					enabled : false
				},
				legend : {
					enabled : false
				},
				series : [ {
					pointPadding : 0,// 数据点之间的距离值
					groupPadding : 0,// 分组之间的距离值
					name : null,
					data : datas
			
				} ]
			});
}

$(function() {
	// swipe 的dome
	$("#container").swipe(
			{
				swipeLeft : function(event, direction, distance, duration,
						fingerCount) {

					now = getNextDay(now, 1);
					charts(now);
				},
				swipeRight : function(event, direction, distance, duration,
						fingerCount) {
					now = getLastDay(now, 1);
					charts(now);
				}
			});

});
function isnull(value) {
	if(value == null || value == "" || value == undefined){
		return null;
	}else{
		return Number(value);
	}
}